
//hide all necessary elements, disable links and forms that have a class name beginning with func, and add ajax actions for those links and forms. 
var allFuncs = new Object();

function buildAjax() {
	hideDivs(); //because prototype can't toggle css styles that have display none, so class style will serve more as a keyword than as a functional hidden div style
	disableLinks();
	disableForms();
}
function hideDivs() {
	var hiddendiv = document.getElementsByClassName('hideme');
	for (y=0; y < hiddendiv.length; y++) {
		$(hiddendiv[y]).hide();
		$(hiddendiv[y]).removeClassName('hideme');
	}
}
function disableLinks() {
	var links = document.getElementsByTagName("a");
	for (i=0; i < links.length; i++) {
		var linkclass = links[i].className;
		if (linkclass.substring(0,4) == "func") links[i].onclick = allFuncs[linkclass.substring(4,linkclass.length)];
	}
}
function disableForms() {
	var forms = document.getElementsByTagName("form");
	for (y=0; y < forms.length; y++) {
		var formclass = forms[y].className;
		if (formclass.substring(0,4) == "func") forms[y].onsubmit = allFuncs[formclass.substring(4,formclass.length)];
	}
}

Event.onDOMReady(function() {
	buildAjax();
});


allFuncs["WidgetForm"] = function() {
	var valid = new Validation('widgetform', {immediate:true});
	if (valid.validate()) {
		new Ajax.Request(self.location+'?ajax=1', {
			parameters: $('widgetform').serialize(true), 
			onLoading: function() { $('widget-loading').toggle('appear'); },
			onLoaded: function() { $('widget-loading').toggle('appear'); },
			onSuccess: function(transport) { $('yourwidget').innerHTML = transport.responseText; },
			onFailure: function(transport) { $('yourwidget').innerHTML = " Ajax Request Failed "; }
		});
	}
	return false;
}